package entities;

import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Id;

import com.googlecode.objectify.annotation.Cached;
import com.googlecode.objectify.annotation.Indexed;
import com.googlecode.objectify.annotation.Unindexed;

@SuppressWarnings("serial")
@Cached
public class ArticleSearch implements Serializable {
	@Id
	private String id = "";

	@Unindexed
	private String title = "";

	@Unindexed
	private String alias = "";

	@Unindexed
	private String content = "";

	@Indexed
	private int status = 1;
	
	@Indexed
	private Date created = new Date();

	@Indexed
	private String creator = "";

	@Indexed
	private String category = "";

	@Unindexed
	private String tags = "";

	@Indexed
	private String language = "en"; // en, es, pt

	@Unindexed
	private String email = "";

	@Unindexed
	private String authorName = "";

	@Unindexed
	private int rate = 3;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAlias() {
		return alias;
	}

	public void setAlias(String alias) {
		this.alias = alias;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Date getCreated() {
		return created;
	}

	public void setCreated(Date created) {
		this.created = created;
	}

	public String getCreator() {
		return creator;
	}

	public void setCreator(String creator) {
		this.creator = creator;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAuthorName() {
		return authorName;
	}

	public void setAuthorName(String authorName) {
		this.authorName = authorName;
	}

	public int getRate() {
		return rate;
	}

	public void setRate(int rate) {
		this.rate = rate;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public static ArticleSearch convert(Article obj) {
		ArticleSearch articleSearch = new ArticleSearch();
		for (Field f : articleSearch.getClass().getDeclaredFields()) {
			f.setAccessible(true);
			try {

				Field fobj = obj.getClass().getDeclaredField(f.getName());
				fobj.setAccessible(true);

				f.set(articleSearch, fobj.get(obj));

			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		return articleSearch;
	}

	public static Article reConvert(ArticleSearch obj) {
		Article article = new Article();

		for (Field f : article.getClass().getDeclaredFields()) {
			f.setAccessible(true);
			try {

				Field fobj = obj.getClass().getDeclaredField(f.getName());
				fobj.setAccessible(true);

				f.set(article, fobj.get(obj));

			} catch (Exception e) {
				// TODO: handle exception
			}
		}

		return article;
	}

	public static List<Article> reConvertList(List<ArticleSearch> articlesSearch) {
		List<Article> articles = new ArrayList<Article>();
		Article article = new Article();

		for (ArticleSearch articleSearch : articlesSearch) {
			if (articleSearch != null) {
				article = reConvert(articleSearch);
				if (article != null)
					articles.add(article);
			}
		}

		return articles;
	}
}